package com.adaxi.adc.business.passengertrip.listener;


import com.adaxi.adc.business.passengertrip.pojo.Response;
import com.adaxi.adc.business.passengertrip.service.IPassengerTripService;
import com.adaxi.adc.business.passengertrip.websocket.WebSocketServer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "response-topic", consumerGroup = "group1")
public class CaroolingDriver implements RocketMQListener <Response>  {
@Autowired
WebSocketServer webSocketServer;
@Autowired
ObjectMapper objectMapper;
@Autowired
IPassengerTripService passengerTripService;

    @Override
    public void onMessage(Response response) {
        try {
            String message = objectMapper.writeValueAsString(response);
            if (webSocketServer != null) {
                String passengerId = response.getPassengerId();

                webSocketServer.sendOneMessage(passengerId, message);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.err.println("将 Request 转换为 JSON 时出错。");
        }
    }
    }
